package com.baizhi.service.impl;


import com.baizhi.dao.AlbumDao;
import com.baizhi.dao.CmfzAudioDao;
import com.baizhi.entity.CmfzAlbum;
import com.baizhi.entity.CmfzAudio;
import com.baizhi.service.CmfzAlbumService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.management.Query;
import java.sql.Wrapper;
import java.util.List;

@Service
@Transactional
public class CmfzAlbumServiceImpl implements CmfzAlbumService {
    @Autowired
    private AlbumDao ad;
    @Override
    @Transactional(readOnly = true)
    public Page<CmfzAlbum> selByPage(int pageNo, int pageSize) {
        Page<CmfzAlbum> page = new Page<>(pageNo, pageSize);
        Page<CmfzAlbum> cmfzAlbumPage = ad.selectPage(page, null);
        return cmfzAlbumPage;
    }

    @Override
    public void insert(CmfzAlbum cmfzAlbum) {
        ad.insert(cmfzAlbum);
    }

    @Override
    public void update(CmfzAlbum cmfzAlbum) {
        ad.updateById(cmfzAlbum);
    }

    @Override
    public void delete(int id) {

        QueryWrapper<CmfzAudio> wrapper = new QueryWrapper();
        wrapper.eq("album_id", id);
        List<CmfzAudio> cmfzAudios = cad.selectList(wrapper);
        for (CmfzAudio cmfzAudio : cmfzAudios) {
            cad.deleteById(cmfzAudio.getAudioId());
        }
        ad.deleteById(id);

    }


    @Autowired()
    private CmfzAudioDao cad;
    @Override
    @Transactional(readOnly = true)
    public List<CmfzAudio> catAlbum(int id) {
        QueryWrapper<CmfzAudio> wrapper = new QueryWrapper();
        wrapper.eq("album_id", id);
        List<CmfzAudio> cmfzAudios = cad.selectList(wrapper);
        return cmfzAudios;
    }
}
